package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.DateUtil;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.IPageDataDAL;
import com.qire.manhua.model.entity.HomeModuleEntity;
import com.qire.manhua.model.entity.HomeModuleEntity.HomeModuleItemEntity;
import com.qire.manhua.model.enumeration.HomeModuleType;
import com.qire.manhua.model.viewModel.impl.RecommendModuleViewModel;
import com.qire.manhua.model.vo.HomeRecommendModuleRefreshVO;
import com.qire.manhua.model.vo.HomeRecommendModuleRefreshVO.HomeModuleItemVO;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class RecommendModuleService extends DataService {

    private IPageDataDAL pageData = WarehouseFactory.create(this, IPageDataDAL.class);
    private RecommendModuleViewModel moduleViewModel;

    public RecommendModuleService(RecommendModuleViewModel moduleViewModel) {
        this.moduleViewModel = moduleViewModel;
    }


    public void nextBatch() {
        if(ObjectUtil.nonNull(moduleViewModel)) {
            pageData.refreshRecommendModuleData(moduleViewModel.moduleId(), moduleViewModel.moduleNextPageNo());
        }
    }

    @StrategyAnnotation(event = Url.HomeRecommendModuleRefresh)
    public void refreshRecommendModuleDataCallBack(HomeRecommendModuleRefreshVO dataVO, boolean isCache) {

        if(ObjectUtil.isNull(dataVO) || ObjectUtil.isNull(dataVO.module) || ObjectUtil.isNull(dataVO.list)) {
            return;
        }

        HomeModuleEntity homeModuleEntity = moduleViewModel.homeModuleEntity();

        if(homeModuleEntity.id != dataVO.module.id) {
            return;
        }

        //limit模块项目数量，刷新的数据不足填满模块
        if(dataVO.list.length < dataVO.module.limit) {
            return;
        }

        homeModuleEntity.setPageNo(dataVO.module.page);

        if(homeModuleEntity.homeModuleType == HomeModuleType.TYPE_1P3) {
            //一带三的布局特殊处理，更新只替换后3个位置
            ArrayList<HomeModuleItemEntity> moduleItemList = homeModuleEntity.moduleItemList();
            HomeModuleItemEntity itemEntity = moduleItemList.get(0);
            moduleItemList.clear();
            moduleItemList.add(itemEntity);
        } else {
            homeModuleEntity.clearModuleItemList();
        }

        for(int index = 0; index < dataVO.list.length; index++) {
            HomeModuleItemVO itemVO = dataVO.list[index];
            int bookId          = itemVO.book_id;
            String bookName     = itemVO.book_name;
            String title        = itemVO.title;
            String shortTitle   = itemVO.short_title;
            String coverImg     = itemVO.cover_img;
            String[] tags       = itemVO.tags;
            String floatTips    = ""; // 空值传递将不会展示
            boolean  isTeensVisible = itemVO.isTeensVisible();

            if(ObjectUtil.nonNull(itemVO.tags)) {
                StringBuilder stringBuilder = new StringBuilder(itemVO.tags.length);
                for (String str : itemVO.tags){
                    stringBuilder.append(str).append(" ");
                }
                shortTitle = stringBuilder.toString();
            }

            if(StringUtil.notEmpty(itemVO.restriction_time)) {
                if(itemVO.restriction_time.length() <= 10) {
                    String timeFormat = "0000-00-00 23:59:59";
                    itemVO.restriction_time = StringUtil.replace(itemVO.restriction_time, timeFormat, 0, timeFormat.length());
                }
//                int daysNumber = DateUtil.yyyy_MM_dd_HHmmss.calcDaysDifferenceToToday(itemVO.restriction_time);
                long daysNumber = DateUtil.yMdHms.toSomeday(itemVO.restriction_time).formatBy(TimeUnit.MILLISECONDS::toDays);
                floatTips = "限免剩余" + daysNumber + "天";
            }

            if(itemVO.free_num > 0) {
                shortTitle = "限免" + itemVO.free_num + "话";
            }

            homeModuleEntity.addModuleItem(bookId, bookName, title, shortTitle, coverImg, tags,
                    0, floatTips, 0, "", isTeensVisible);
        }

        moduleViewModel.refreshNextBatch();
    }


}
